package com.asa.mail;

import java.util.regex.*;

public class mailReplace {
	public String htmlRelace(String html, String root, String mail, String pid) {
		
		String patHref = "href=(\"|'|)([^\"'<>\\s]+)(\"|'|)";
		String patMail = "\\$\\{mail\\}";
		
		Pattern pattern = Pattern.compile(patHref);
		Matcher m = pattern.matcher(html);
		StringBuffer buffer = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(buffer, "href=\"" + root + "?url=" + m.group(2)
					+ "&pid=" + pid + "&mail=" + mail);
		}
		m.appendTail(buffer);
		return buffer.toString().replaceAll(patMail, mail);

	}

	public static void main(String[] args) {

		String html = "<a href=\"126.com\">test</a> \r\n 777<img src=\"${mail}\"";
		mailReplace test = new mailReplace();
		System.out.print(test.htmlRelace(html,
				"http://www.chanhou.com/mai-c.php", "snoopyone@126.com", "1"));

	}

}
